<?php
/**
 * Created by PhpStorm.
 * User: 颖inv
 * Date: 2016/10/12
 * Time: 20:28
 */
header('content-type:text/html; charset=utf-8');
$link=mysqli_connect('localhost','root','');
if(!$link){
    die('连接数据库失败！'.mysqli_error($link));
}

mysqli_query($link,'set names utf8');
mysqli_query($link,'use `itcast`');

//员工信息排序
$fields=array('e_dept','date_of_entry');//定义合法的排序字段
//初始化
$sql_order='';
$order=isset($_GET['order'])?$_GET['order']:'';
$sort=isset($_GET['sort'])?$_GET['sort']:'';
//判断$order是否存在于合法字段列表$fields中
if (in_array($order,$fields)){
    if($sort=='desc'){
        $sql_order="order by $order desc";
        $sort='asc';
    }else{
        $sql_order="order by $order asc";
        $sort='desc';
    }
}



//员工信息搜索
$where='';
if(isset($_GET['keyword'])){
    $keyword=$_GET['keyword'];
    $keyword=mysqli_real_escape_string($link,$keyword);
    $where="where e_name like '%$keyword%' or e_dept like '%$keyword%'";
}


//分页显示信息
$page_size=3;
$result=mysqli_query($link,'select count(*) from `emp_info`');
$count=mysqli_fetch_row($result);
$count=$count[0];
$max_page=ceil($count/$page_size);
$page=isset($_GET['page'])?intval($_GET['page']):1;
$page=$page>$max_page?$max_page:$page;
$page=$page<1 ? 1 : $page;
$page_html="<a href='./showList.php?page=1'>首页</a>&nbsp;";
$page_html.="<a href='./showList.php?page=".(($page-1)>0?($page-1):1)."'>上一页</a>&nbsp;";
$page_html.="<a href='./showList.php?page=".(($page+1)<$max_page?($page+1):$max_page)."'>下一页</a>&nbsp;";
$page_html.="<a href='./showList.php?page={$max_page}'>尾页</a>";
$lim=($page-1)*$page_size;


$sql="select* from `emp_info` $sql_order $where limit $lim,$page_size";
$result=mysqli_query($link,$sql);
$emp_info=array();
while ($row=mysqli_fetch_assoc($result)){
$emp_info[]=$row;}

//require './public_function.php';
//dbInit();
//$sql='select * from `emp_info`';
//$emp_info=fetchAll($sql);

define('APP','itcast');
require './list_html.php';
?>
